/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package qrlogin.servlets;

import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.encoder.ByteMatrix;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author adee
 */
@WebServlet(name="Generator", urlPatterns={"/Generator.do"})
public class Generator extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
        byte[] b = {0x48, 0x45, 0x4C, 0x4C, 0x4F};
        int width=120;
        int height = 120;
        //convert the byte array into a UTF-8 string
        String data;
          try {
           data = new String(b, "UTF8");
          }
          catch (UnsupportedEncodingException e) {
           //the program shouldn't be able to get here
          return;
         }

        data = request.getSession().getId();

         //get a byte matrix for the data
         BitMatrix matrix;
         com.google.zxing.Writer writer = new QRCodeWriter();
        try {
           matrix = writer.encode(data, com.google.zxing.BarcodeFormat.QR_CODE, width, height);
         }
        catch (com.google.zxing.WriterException e) {
          //exit the method
          return;
         }

          //generate an image from the byte matrix
           
           width = matrix.getWidth();
           height = matrix.getHeight();
           //byte[][] array = new byte[width][height];


          //create buffered image to draw to
          BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

          //iterate through the matrix and draw the pixels to the image
         for (int y = 0; y < height; y++) {
          for (int x = 0; x < width; x++) {
           int grayValue = matrix.get(x, y)? 0:0xffffffff;
           image.setRGB(x, y,grayValue);
         }
        }

        //write the image to the output stream


         String path= request.getServletContext().getRealPath("");
         String file = path+"/kep.png";
         FileOutputStream outputStream = new FileOutputStream(new File(file));


        ImageIO.write(image, "png", outputStream);


        out.println("<img src=\"/QRLoginTest/kep.png\" />");
           
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
